﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using WxStockWinSvc.Models.Enums;
using System.IO;
using WxStockWinSvc.Commons;
using WxStockWinSvc.Models.Entities;

namespace WxStockWinSvc.DALs.Http
{
    class Dal163StockData
    {
        //http://quotes.money.163.com/service/chddata.html?code=1002370&start=20100316&end=20121122&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP
        const string _reqUrlTemplate = @"http://quotes.money.163.com/service/chddata.html?code={0}{1}&start={2}&end={3}&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP";

        internal static List<string> LoadStockInfoSpecifyDate(StockInfo4Load stock4Load, DateTime endTime)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(GenerateRequestURL4AllPeriod(stock4Load.ExchangeCode
                , stock4Load.StockCode
                , stock4Load.MaxDate
                , endTime));

            req.Headers[HttpRequestHeader.CacheControl] = "no-cache";
            req.Accept = "text/html, application/xhtml+xml, */*";
            req.Headers[HttpRequestHeader.AcceptCharset] = "utf-8;q=0.7,*;q=0.3";
            req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";

            req.Headers[HttpRequestHeader.AcceptLanguage] = "zh-CN";
            req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";

            if (ServicePointManager.DefaultConnectionLimit != CommonUtil.LoadStockThreadCount)
                ServicePointManager.DefaultConnectionLimit = CommonUtil.LoadStockThreadCount;

            List<string> listResult = new List<string>();
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("GB2312")))
            {
                req.ServicePoint.CloseConnectionGroup(req.ConnectionGroupName);//这句是用来做什么的？？

                string sLine = string.Empty;
                while ((sLine = sr.ReadLine()) != null)
                    listResult.Add(sLine);
            }

            return listResult;
        }

        static string GenerateRequestURL4AllPeriod(ChinaExchangeCategory exCate, string stockCode, DateTime startTime, DateTime endTime)
        {
            string sUrl = string.Format(_reqUrlTemplate
                , exCate.ToString("d")
                , stockCode
                , startTime.ToString("yyyyMMdd")
                , endTime.AddDays(1).ToString("yyyyMMdd"));

            return sUrl;
        }

        static string GenerateRequestURL(ChinaExchangeCategory exCate, string stockCode, DateTime startTime, DateTime endTime)
        {
            string sUrl = string.Format(_reqUrlTemplate
                , exCate.ToString("d")
                , stockCode
                , startTime.ToString("yyyyMMdd")
                , endTime.ToString("yyyyMMdd"));

            return sUrl;
        }
    }
}
